package service

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"lot_admin_api/api"
	"lot_admin_api/internal/consts"
	"lot_admin_api/internal/dao"
	"lot_admin_api/internal/model"
	GenInt64Id "lot_admin_api/utility/genInt64IdOfString"
	"lot_admin_api/utility/paging"
	"lot_admin_api/utility/response"
)

type sEquipment struct{}

var insEquipment = sEquipment{}

func Equipment() *sEquipment {
	return &insEquipment
}

// AddEquipment 新增设备
func (s *sEquipment) AddEquipment(ctx context.Context, req *api.AddEquipmentReq) (lastInsertId string, err error) {
	r := g.RequestFromCtx(ctx)
	if findOne, _ := dao.LotEquipment.Ctx(ctx).Where("eq_code", req.EqCode).One(); findOne != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "已有重复的设备")
	}
	var saveInfo *model.EquipmentIdAndInfoRes
	_ = gconv.Scan(req, &saveInfo)
	saveInfo.Id = GenInt64Id.GenInt64IdOfString()      // 生成雪花id
	_, _ = Site().SearchSiteInfo(ctx, saveInfo.SiteId) // 查看站点是否存在
	if _, err = dao.LotEquipment.Ctx(ctx).Save(saveInfo); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "保存设备失败")
	}
	BatchPort := &model.BatchAddEquipmentPortReq{
		EqId:    saveInfo.Id,
		PortNum: saveInfo.EqPorts,
	}
	// 批量增加端口
	if _, err = EquipmentPort().AddEquipment(ctx, BatchPort); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), err.Error())
	}
	return saveInfo.Id, err
}

// EditEquipment 编辑设备
func (s *sEquipment) EditEquipment(ctx context.Context, req *api.EditEquipmentReq) (lastInsertId string, err error) {
	r := g.RequestFromCtx(ctx)
	_, _ = s.SearchEquipmentInfo(ctx, req.Id) // 查看设备是否存在
	if _, err = dao.LotEquipment.Ctx(ctx).OmitEmpty().FieldsEx("eq_code,eq_ports").Where("id", req.Id).Update(&req); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "更新设备失败")
	}
	return req.Id, err
}

// SearchEquipmentInfo 通过id查询设备信息
func (s *sEquipment) SearchEquipmentInfo(ctx context.Context, id string) (res *model.EquipmentDbInfoPortRes, err error) {
	r := g.RequestFromCtx(ctx)
	err = dao.LotEquipment.Ctx(ctx).WithAll().Where("id", id).Scan(&res)
	if res == nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "没有该设备")
	}
	return res, err
}

// DeleteEquipment 删除设备
func (s *sEquipment) DeleteEquipment(ctx context.Context, req *api.DeleteEquipmentReq) (id string, err error) {
	r := g.RequestFromCtx(ctx)
	_, _ = s.SearchEquipmentInfo(ctx, req.Id) // 查看设备是否存在
	if _, err = dao.LotEquipment.Ctx(ctx).Where("id", req.Id).Delete(); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "没有该设备")
	}
	return req.Id, err
}

// EquipmentList 查询设备列表
func (s *sEquipment) EquipmentList(ctx context.Context, req *api.EquipmentListReq) (res *model.EquipmentListPageRes, err error) {
	r := g.RequestFromCtx(ctx)
	EquipmentList := make([]*model.EquipmentDbInfoPortRes, 0)
	// 获取分页偏移量
	startNum, endNum := paging.Paging(req.Paging)
	searchInfo := g.Map{
		"site_id":        req.SiteId,
		"eq_code like ?": "%" + req.EqCode + "%",
		"eq_name like ?": "%" + req.EqName + "%",
	}
	if err = dao.LotEquipment.Ctx(ctx).WithAll().OmitEmpty().Where(searchInfo).Limit(startNum, endNum).Scan(&EquipmentList); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "查找设备列表失败")
	}
	countInfo, err := dao.LotEquipment.Ctx(ctx).OmitEmpty().Where(searchInfo).Count()
	putCount := paging.PutCount(req.Paging, countInfo)
	res = &model.EquipmentListPageRes{
		List:    EquipmentList,
		PutPage: *putCount,
	}
	return res, err
}
